package com.example.Utils;

import com.example.Entity.Vo.APIResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.internet.MimeMessage;
import java.io.File;

@Component
public class SendEmailUtils {
    @Autowired
    JavaMailSender javaMailSender;

    /**
     * 发送邮件带有抄送人
     * @param from 邮件发送者
     * @param to 收件人
     * @param cc 抄送人
     * @param subject 邮件主题
     * @param content 邮件内容
     * @return
     */
    public APIResult SendEmailType1(String from, String to, String cc, String subject, String content){
        try {
            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
            simpleMailMessage.setFrom(from);
            simpleMailMessage.setTo(to);
            simpleMailMessage.setCc(cc);
            simpleMailMessage.setSubject(subject);
            simpleMailMessage.setText(content);
            javaMailSender.send(simpleMailMessage);
            return APIResultUtils.success("邮件发送成功");
        }
        catch (Exception e){
            return APIResultUtils.error("发送邮件出现错误",e.getMessage());
        }
    }

    /**
     * 发送邮件不带抄送人
     * @param from 邮件发送者
     * @param to 收件人
     * @param subject 邮件主题
     * @param content 邮件内容
     * @return
     */
    public APIResult SendEmailType2(String from,String to,String subject,String content){
        try {
            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
            simpleMailMessage.setFrom(from);
            simpleMailMessage.setTo(to);
            simpleMailMessage.setSubject(subject);
            simpleMailMessage.setText(content);
            javaMailSender.send(simpleMailMessage);
            return APIResultUtils.success("邮件发送成功");
        }
        catch (Exception e){
            return APIResultUtils.error("发送邮件出现错误",e.getMessage());
        }
    }

    /**
     * 发送邮件不带抄送人和主题
     * @param from 邮件发送者
     * @param to 收件人
     * @param content 邮件内容
     * @return
     */
    public APIResult SendEmailType3(String from, String to, String content){
        try {
            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
            simpleMailMessage.setFrom(from);
            simpleMailMessage.setTo(to);
            simpleMailMessage.setText(content);
            javaMailSender.send(simpleMailMessage);
            return APIResultUtils.success("邮件发送成功");
        }
        catch (Exception e){
            return APIResultUtils.error("发送邮件出现错误",e.getMessage());
        }
    }

    /**
     * 发送邮件不带抄送人和主题，这是一个带文件的发送
     * @param from 邮件发送者
     * @param to 收件人
     * @param content 邮件内容
     * @param file 发送发送的文件
     * @return
     */
    public APIResult SendEmailType4(String from, String to, String content, File file){
        try {
            MimeMessage message = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message,true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setText(content);
            helper.addAttachment(file.getName(),file);
            javaMailSender.send(message);
            return APIResultUtils.success("邮件发送成功");
        }
        catch (Exception e){
            return APIResultUtils.error("发送邮件出现错误",e.getMessage());
        }
    }

    /**
     * 发送html模板的邮件
     * @param from 邮件发送者
     * @param to 收件人
     * @param subject 邮件主题
     * @param content 邮件内容（html）转换成的string
     * @return
     */
    public APIResult SendEmailTemplate(String from,String to,String subject,String content){
        try {
            MimeMessage message = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message,true);
            helper.setTo(to);
            helper.setFrom(from);
            helper.setSubject(subject);
            helper.setText(content,true);
            javaMailSender.send(message);
            return APIResultUtils.success("发送成功");
        }
        catch (Exception e){
            return APIResultUtils.error("发送失败",e.getMessage());
        }
    }
}

